package com.accompany.api.presenter;
import com.accompany.api.bean.UserLoginBean;
import com.accompany.api.category.UserLoginCategory;
import com.accompany.base.BaseBean;
import com.accompany.utils.RetrofitUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
 * 信息说明:
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/3/21 17 ：12
 * 项目名称：yuanban
 */
public class UserLoginPresenter implements UserLoginCategory.Presenter {
    UserLoginCategory.View view;
    public UserLoginPresenter(UserLoginCategory.View view) {
        this.view = view;
    }
    /**
     * @param mobile 根据手机号码获取验证码
     */
    @Override
    public void getUserCode(String mobile) {
        RetrofitUtil.getInstance().getmService().getUserCode(mobile).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    BaseBean userInfor = gson.fromJson(result, BaseBean.class);
                    view.getUserCode(userInfor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }
    /**
     * @param mobile
     * @param smsCode
     * 根据手机号码、验证码登录
     */
    @Override
    public void uSerLogin(String mobile, String smsCode) {
        RetrofitUtil.getInstance().getmService().getUserLogin(mobile, smsCode).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    UserLoginBean userInfor = gson.fromJson(result, UserLoginBean.class);
                    view.uSerLogin(userInfor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }
}
